package com.mybatis.lo.handler;

import com.mybatis.lo.config.MappedStatement;
import com.mybatis.lo.mapping.BoundSql;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * 通过BoundSql获取到sql，不进行额外参数处理，直接通过jdbc执行sql的处理器
 * @author zhangbh
 * 2023-06-20 09:16
 **/
public class SimpleStatementHandler implements StatementHandler {

    private final MappedStatement mappedStatement;
    private final ResultSetHandler resultSetHandler;
    private final BoundSql boundSql;
    private final Object param;

    public SimpleStatementHandler(MappedStatement mappedStatement, ResultSetHandler resultSetHandler, BoundSql boundSql, Object param) {
        this.mappedStatement = mappedStatement;
        this.resultSetHandler = resultSetHandler;
        this.boundSql = boundSql;
        this.param = param;
    }

    @Override
    public Statement prepare(Connection connection) throws SQLException {
        System.out.println("查询sql=" + boundSql.getSql() + ",查询参数=" + boundSql.getParameterMappings());
        return connection.createStatement();
    }

    @Override
    public void parameterize(Statement statement) throws SQLException {
        // 没有参数需要设置
    }

    @Override
    public <T> List<T> query(Statement statement) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        statement.execute(boundSql.getSql());
        return resultSetHandler.handleResultSets(statement);
    }
}
